package com.controleFamiliar.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.controleFamiliar.pojo.CategoriaDespesa;
import com.controleFamiliar.pojo.Despesa;
import com.controleFamiliar.util.HibernateUtil;

public class DespesaDAO extends GenericDAO<Integer, Despesa>{
	
	public void salvarDespesa(Despesa despesa) throws Exception {
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			Despesa novaDespesa = new Despesa();
			novaDespesa = despesa;
			session.save(novaDespesa);
			session.flush();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
	}
	
	public void removerDespesa(Integer codDespesa) throws Exception {
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			String hql = "Delete From Despesa where codDespesa = :codDespesa";
			Query query = session.createQuery(hql);
			query.setInteger("codDespesa", codDespesa);
			int result = query.executeUpdate();
			System.out.println("Rows affected: " + result);
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Despesa> listarTodasDespesas(){
		List<Despesa> lista = new ArrayList<Despesa>();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM Despesa");
			Query q = session.createQuery(sb.toString());
			lista = q.list();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Despesa> listarDespesasPorCategoria(Integer codigo){
		List<Despesa> lista = new ArrayList<Despesa>();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM Despesa where categoriaDespesa.codCategoriaDespesa = :codigo");			
			Query q = session.createQuery(sb.toString());
			q.setInteger("codigo", codigo);
			lista = q.list();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Despesa> listarDespesasPorPeriodo(Date dtInicio, Date dtFim){
		List<Despesa> lista = new ArrayList<Despesa>();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM Despesa where dataDespesa >= :dtInicio and dataDespesa <= :dtFim");			
			Query q = session.createQuery(sb.toString());
			q.setDate("dtInicio", dtInicio);
			q.setDate("dtFim", dtFim);
			lista = q.list();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return lista;
	}
	
	
	public Despesa obterDespesa(Integer codDespesa){
		Despesa despesa = new Despesa();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM Despesa where codDespesa = :codDespesa");			
			Query q = session.createQuery(sb.toString());
			q.setInteger("codDespesa", codDespesa);
			despesa = (Despesa)q.uniqueResult();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return despesa;
	}

}
